#include "stdafx.h"

#include "hwindef.h"
#include "hwinstring.h"
#include "hwinexception.h"
#include <iostream>

namespace harlinn
{
    namespace windows
    {
        HWIN_EXPORT const String& String::CopyTo( wchar_t* buffer, size_type bufferSize, size_type start, wchar_t padCharacter ) const
        {
            if(!buffer)
            {
                throw ArgumentNullException("buffer");
            }

            Buffer* thisBuffer = toBuffer();
            if( thisBuffer && (start < thisBuffer->length))
            {
                size_type copyLength = bufferSize;
                size_type padLength = 0;

                if(copyLength > (thisBuffer->length - start))
                {
                    copyLength = thisBuffer->length - start;
                    padLength = bufferSize - copyLength;
                }
                std::copy_n(data+start,copyLength,buffer);
                std::fill_n(buffer,padLength,padCharacter);
            }
            else
            {
                std::fill_n(buffer,bufferSize,padCharacter);
            }

            return *this;
        }

        HWIN_EXPORT std::wostream& operator << ( std::wostream& os, const String& s)
        {
            if(s.data)
            {
                os.write(s.data,s.length());
            }
            return os;
        }


    };
};